<?php

namespace App\Http\Controllers\VideoApi;

use App\Http\Config\ErrorCode;
use App\Models\video\CatModel;
use App\Models\video\SearchLogModel;
use App\Models\video\VodVideoModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class SearchController extends BaseController
{
    public function __construct(Request $request)
    {
        $this->noCheckTokenAction = ['getSearchContent', 'getSearchLogData', 'getCatList', 'getAllCat']; //不校验openid
        parent::__construct($request);
    }

    // 获取搜索记录
    public function getSearchLogData(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        if(!$request->token){ // 未登录
            $logData = SearchLogModel::getFields("content", ["user_id" => 0], false, false);
        }else{
            $userId = $this->getTokenVar($request->token, "id");
            $logData = SearchLogModel::getFields("content", ["user_id" => $userId], false, false);
        }
        $content = [];
        foreach ($logData as $item){
            $content[] = urldecode($item["content"]);
        }
        $this->returnData["data"] = array_reverse($content);
        return $this->return_result($this->returnData);
    }

    // 搜索内容视频
    public function getSearchContent(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        if(!$request->searchText){
            return $this->return_result(ErrorCode::code("customized", "搜索内容不能为空"));
        }
        $insertLog = ["content" => $request->searchText, "mark" => $request->mark ?: null];
        if(!$request->token){ // 未登录
            $logData = SearchLogModel::getFields("id", ["user_id" => 0, "content" => $request->searchText], true, false);
            $insertLog["user_id"] = 0;
        }else{
            $userId = $this->getTokenVar($request->token, "id");
            $logData = SearchLogModel::getFields("id", ["user_id" => $userId, "content" => $request->searchText], true, false);
            $insertLog["user_id"] = $userId;
        }
        if(!empty($logData)){
            SearchLogModel::doUpdate(["count" => DB::raw("count+1")], ["id" => $logData["id"]]);
        }else{
            SearchLogModel::toAdd($insertLog);
        }
        $db = VodVideoModel::query()->where("title", "like", "%{$request->searchText}%")
            ->where(["handle_result" => 2, "status" => 1, "mall_id" => $request->mallData["id"]])
            ->select(["id","title","cover_img_id","desc","view_count","video_length", "broadcaster_id", "create_time", "file_type"]);
        $page = $request->page ?: 1;
        $pageSize = $request->pageSize ?: 20;
        $sortName = $request->sortName ? Str::snake($request->sortName) : "create_time";
        $sortOrder = $request->sortOrder ?: "desc";
        $data['count'] = $db->count();
        $data['currentPage'] = $page;
        $data['totalPage'] = ceil(($data['count'])/$pageSize);
        $data['list'] = $db->skip(($page - 1) * $pageSize)->take($pageSize)->orderBy($sortName, $sortOrder)->get();
        if($data['list']){
            $data['list'] = VodVideoModel::transferField($data['list']->toArray());
        }
        $this->returnData["data"] = $data;
        return $this->return_result($this->returnData);
    }

    public function getAllCat(Request $request){
        if($this->returnData["code"] !== 0){
            return $this->return_result($this->returnData);
        }
        $this->returnData["data"] = CatModel::query()->select("id", "name", "icon_id")
            ->where(["mall_id" => $request->mallData["id"], "status" => 1])
            ->orderBy("sort", "desc")
            ->get()->toArray();
        if(!empty($this->returnData["data"])){
            $this->returnData["data"] = CatModel::transferField($this->returnData["data"]);
        }else{
            $this->returnData["data"] = [['id' => 0, "name" => "默认", "icon_id" => ""]];
        }
        return $this->return_result($this->returnData);
    }
}
